<template>
  <div class="single-correct" @click="hiddenRecordList">
    <CorrectHeader
      :qtsIndexData="qtsIndexData"
      :studenName="currrentStuData?.student_name"
      @showRecordList="isShowRecordList = true"
    ></CorrectHeader>
    <QtsMarking
      :qtsIndexData="qtsIndexData"
      @getCurrentStuInfo="getCurrentStuInfo"
    ></QtsMarking>
    <RecordList
      v-if="isShowRecordList"
      @hiddenRecordList="hiddenRecordList"
    ></RecordList>
  </div>
</template>

<script setup>
import { ref, onMounted, onBeforeUnmount } from 'vue'
import $bus from '@/utils/tools/bus'
import CorrectHeader from '@/views/single-correct/components/CorrectHeader.vue'
import QtsMarking from '@/views/single-correct/components/QtsMarking.vue'
import RecordList from '@/views/single-correct/components/RecordList.vue'
import { getTaskTopic } from '@/apis/single-correct/index'
import { useSingleQtsCorrectStore } from '@/stores/single-correct'
const singleQtsCorrectStore = useSingleQtsCorrectStore()

const qtsIndexData = ref([])
const currrentStuData = ref()
const isShowRecordList = ref(false)

onMounted(async () => {
  singleQtsCorrectStore.isRemark = false
  await getQtsIndexData()
  initSelectQts()
})

$bus.on('update-qts-index', async () => {
  await getQtsIndexData()
  let qts = singleQtsCorrectStore.currentQtsData
  for (let i = 0; i < qtsIndexData.value.length; i++) {
    let item = qtsIndexData.value[i]
    if (
      item.question_id == singleQtsCorrectStore.currentQtsData.question_id &&
      !item.sub_question_info
    ) {
      qts = item
      break
    }
    if (item.sub_question_info && Array.isArray(item.sub_question_info)) {
      const subQts = item.sub_question_info.find(
        subItem =>
          subItem.question_id ==
          singleQtsCorrectStore.currentQtsData.question_id
      )
      if (subQts) {
        qts = subQts
        break
      }
    }
  }
  singleQtsCorrectStore.currentQtsData = qts
})

const getQtsIndexData = async () => {
  let res = await getTaskTopic({
    task_id: singleQtsCorrectStore.taskId,
    class_id: singleQtsCorrectStore.classId,
    class_type: singleQtsCorrectStore.classType
  })
  if (res.code == 1 && res.data.length > 0) {
    qtsIndexData.value = res.data.map((v, index) => {
      v.item_type = 1
      v.order_text = `题${index + 1}：${v.question_type}`
      if (v.sub_question_info && Array.isArray(v.sub_question_info)) {
        v.sub_question_info.forEach((s, sIndex) => {
          s.item_type = 2
          s.father_id = v.question_detail.id
          let father = JSON.parse(JSON.stringify(v.question_detail))
          delete father.subproblem
          s.father_detail = father
          s.question_detail = v.question_detail.subproblem[sIndex]
          s.order_text = `${index + 1}.${sIndex + 1}`
          // v.order_text = `${index + 1}.${sIndex + 1}：${s.question_type}`
        })
      }
      return v
    })
  }
}

//初始化 选中第一个没批改的题目
const initSelectQts = () => {
  let qts = null
  for (let i = 0; i < qtsIndexData.value.length; i++) {
    let item = qtsIndexData.value[i]
    if (item.await_grade !== 0 && !item.sub_question_info) {
      qts = item
      break
    }
    if (item.sub_question_info && Array.isArray(item.sub_question_info)) {
      const subQts = item.sub_question_info.find(
        subItem => subItem.await_grade !== 0
      )
      if (subQts) {
        qts = subQts
        break
      }
    }
  }

  const firstIndexData = qtsIndexData.value[0]
  singleQtsCorrectStore.currentQtsData =
    qts ||
    (firstIndexData.sub_question_info
      ? firstIndexData.sub_question_info[0]
      : firstIndexData)
  $bus.emit('on-select-qts')
}

const getCurrentStuInfo = info => {
  currrentStuData.value = info
}

const hiddenRecordList = () => {
  isShowRecordList.value = false
}

onBeforeUnmount(() => {
  $bus.off('update-qts-index')
})
</script>
